﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ForumDB
{
	class ThreadQuery : Query
	{
		System.Exception errorT = new System.Exception("\n\n**OUT OF RANGE**\n");
		IQueryable<Thread> _threads;

		public ThreadQuery(IQueryable query)
		{
			_threads = query.Cast<Thread>();
		}

		public void setTypeQuery(IQueryable query)
		{
			_threads = query.Cast<Thread>();
		}

		public void getInfo()
		{
			foreach (var thread in _threads)
			{
				Console.WriteLine("ID={0}, Description={1}", thread.ThreadID, thread.Description);
			}
		}





		public String getInfo2()
		{
			String info = "";
			foreach (var thread in _threads)
			{
				info = info + "<br/>" + thread.ThreadID + ", " + thread.Description + ", "
				+ thread._SubForumID + ", " + thread.Title + ", " + thread.Body + ", " + thread._ClientID + ", " + thread.DateTime;
			}
			return info;
		}





		public String getInfo3()
		{
			foreach (var thread in _threads)
			{
				Console.WriteLine("ID={0}, Description={1}", thread.ThreadID, thread.Description);
			}

			return "ff";
		}


//*****************************************************************************************************
//*****************************************************************************************************

		public override String getThreadInfo()
		{
			String info = "";
			foreach (var thread in _threads)
			{
				info = info + "<br/>" + thread.ThreadID + ", " + thread.Description + ", "
				+ thread._SubForumID + ", " + thread.Title + ", " + thread.Body + ", " + thread._ClientID + ", " + thread.DateTime;
			}
			return info;
		}



//starting from here:
//*********************** shared in all objects **********************************************************************

		public override int getId(int numberInTable)
		{
			Thread tmpThread = getValidLine(numberInTable);
			if (tmpThread != null)
				return tmpThread.ThreadID;
			throw errorT;
		}


		public override String getTitle(int numberInTable)
		{
			Thread tmpThread = getValidLine(numberInTable);
			if (tmpThread != null)
				return tmpThread.Title;
			throw errorT;
		}


//helper methods

		public Thread getValidLine(int numberInTable)
		{
			if (_threads.Count() != 0 && numberInTable <= _threads.Count())
				return _threads.Skip(numberInTable - 1).Take(1).Single();
			return null;
		}
		


	}
}
